﻿using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace PdfOperator
{
    public partial class FrmPdfCut : Form
    {
        public FrmPdfCut()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择PDF文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenfile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "打开PDF";
            fileDialog.Filter = "pdf文件|*.pdf";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string name = fileDialog.FileName;
                txtFilePath.Text = name;
            }
        }


        /// <summary>
        /// 按页分割方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdoUsePage_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoUsePage.Checked == true)
            {
                txtStartPage.Enabled = true;
                txtEndPage.Enabled = true;
            }
        }

        /// <summary>
        /// 单页分割方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdoOnePage_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoOnePage.Checked == true)
            {
                txtStartPage.Enabled = false;
                txtEndPage.Enabled = false;
            }
        }

        /// <summary>
        /// 选择保存目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSavePath_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请先选择要操作的pdf！");
                return;
            }
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.Description = "请选择文件路径";
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                txtSavePath.Text = folderBrowser.SelectedPath;
            }

        }

        /// <summary>
        /// 开始分割PDF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            //string extension = Path.GetExtension(txtFilePath.Text).Trim();
            //if (extension.ToLower() != ".pdf") { MessageBox.Show("请先选择pdf文件！"); return; }

            int startPage = 0;
            int endPage = 0;
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请先选择要操作的pdf！");
                return;
            }
            if (string.IsNullOrEmpty(txtSavePath.Text))
            {
                MessageBox.Show("请先选择要保存路径！");
                return;
            }

            // 每页拆分为一个pdf
            if (rdoOnePage.Checked)
            {
                PdfDocument doc = new PdfDocument(txtFilePath.Text);
                string pattern = txtSavePath.Text + "\\分割的PDF{0}.pdf";
                doc.Split(pattern);
                doc.Close();
                MessageBox.Show("完成");
            }

            // 按页分割
            if (rdoUsePage.Checked)
            {
                try
                {
                    startPage = int.Parse(txtStartPage.Text);
                    endPage = int.Parse(txtEndPage.Text);
                }
                catch (Exception)
                {
                    MessageBox.Show("页码输入格式有误！");
                    startPage = 0;
                    endPage = 0;
                }

                //加载一个现有的PDF文档
                PdfDocument pdf = new PdfDocument();
                pdf.LoadFromFile(txtFilePath.Text);
                //新建1个PDF文档
                PdfDocument pdf1 = new PdfDocument();

                PdfPageBase page;
                //将现有PDF文档的第startPage-endPage页拆分为一个文档
                for (int i = startPage - 1; i < endPage; i++)
                {
                    //向新建文档添加与现有文档页面大小一致的页面
                    page = pdf1.Pages.Add(pdf.Pages[i].Size, new PdfMargins(0));
                    //为现有文档的页面创建模板并将模板画到新建文档的页面上
                    pdf.Pages[i].CreateTemplate().Draw(page, new PointF(0, 0));
                }

                //保存文档
                pdf1.SaveToFile(txtSavePath.Text + "\\" + startPage.ToString() + "-" + endPage.ToString() + ".pdf");
                MessageBox.Show("完成");
            }
        }


        /// <summary>
        /// 开始拖放文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFilePath_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
                this.txtFilePath.Cursor = Cursors.Arrow; //指定鼠标形状   
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }



        /// <summary>
        /// 接收拖放文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFilePath_DragDrop(object sender, DragEventArgs e)
        {
            //DataFormats 数据的格式，下有多个静态属性都为string型，除FileDrop格式外还有Bitmap,Text,WaveAudio等格式    
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            if (Path.GetExtension(path).ToLower() == ".pdf")
            {
                this.txtFilePath.Text = path;
            }
            this.txtFilePath.Cursor = Cursors.IBeam; //还原鼠标形状 
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
